package com.example.demo.module.user.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.common.service.impl.BaseServiceImpl;
import com.example.demo.entity.pojo.user.UserPoJo;
import com.example.demo.module.user.mapper.UserMapper;
import com.example.demo.module.user.service.UserService;
import com.example.demo.common.service.impl.BaseServiceImpl;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * .
 *
 * @author Li Zhiyuan
 * @since 2020/8/21
 */
@Service
public class UserServiceImpl extends BaseServiceImpl<UserMapper, UserPoJo> implements UserService {
    @Resource
    private UserMapper mapper;

    @Override
    public long create(String name, String password) {
        UserPoJo userPoJo = new UserPoJo();
        userPoJo.setName(name);
        userPoJo.setPassword(password);
        return this.create(userPoJo).getId();
    }

    @Override
    public UserPoJo selectOne(UserPoJo poJo) {
        QueryWrapper<UserPoJo> queryWrapper = Wrappers.query(poJo);
        return mapper.selectOne(queryWrapper);
    }

    @Override
    @Cacheable(key = "UserPoJoAll")
    public List selectAll() {
        QueryWrapper<UserPoJo> queryWrapper = Wrappers.query();
        List<UserPoJo> list = mapper.selectList(queryWrapper);
        return list;
    }

    @Override
    public Page<UserPoJo> page(String keywords, int page, int size) {
        return this.page(
                Wrappers.<UserPoJo>lambdaQuery()
                        .like(UserPoJo::getName, keywords),
                page,
                size,
                OrderItem.asc("create_time"), OrderItem.asc("password"));
    }
}
